package com.vueapi.controller.admin;

import com.vueapi.annotation.OperationLog;
import com.vueapi.domain.HjTag;
import com.vueapi.service.HjTagService;
import com.vueapi.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @Classname AdminTagController
 * @Description TODO
 * @Created by 赵宏建
 * @Date 2022/9/21 15:37
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/admin/tag")
public class AdminTagController {


    @Resource
    private HjTagService tagService;

    @GetMapping("/list")
    public ResultUtils list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            @RequestParam(required = false, defaultValue = "") String tagSearch) {
        return tagService.listPage(pageNum, pageSize, tagSearch);
    }

    @OperationLog(name = "新增标签")
    @PostMapping("/add")
    public ResultUtils add(@RequestBody HjTag tag) {
        tagService.save(tag);
        return ResultUtils.success("添加成功", null);
    }

    @OperationLog(name = "修改标签")
    @PutMapping("/edit")
    public ResultUtils edit(@RequestBody HjTag tag) {
        tagService.updateById(tag);
        return ResultUtils.success("更新成功", null);
    }

    @OperationLog(name = "删除标签")
    @DeleteMapping("/delete")
    public ResultUtils delete(@RequestParam String ids) {
        List<String> useTagIds = tagService.checkAndRemoveByIds(ids);
        if(useTagIds.isEmpty()){
            return ResultUtils.success();
        }
        return ResultUtils.fail(useTagIds + "标签正在被使用，不能删除！");
    }

    @OperationLog(name = "修改标签是否置顶")
    @PostMapping("/top")
    public ResultUtils top(@RequestBody HjTag tag) {
        return tagService.top(tag);
    }

    @GetMapping("/getAllTag")
    public ResultUtils getAllTag() {
        return ResultUtils.success(tagService.list());
    }

    @GetMapping("/getTagCount")
    public ResultUtils getTagCount() {
        return ResultUtils.success(tagService.getTagCount());
    }
}
